/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelEditorTowers.java
 *
 * Created on Aug 19, 2009, 9:36:38 PM
 */
package hextd.UI;

import hextd.core.Context;
import hextd.core.ContextNormal;
import hextd.hex.HexTile;
import hextd.map.JsonTower;
import hextd.map.JsonTowerOptions;
import hextd.tower.HexTDTower;
import hextd.tower.FactoryTower;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;

/**
 *
 * @author hylke
 */
public class PanelEditorTowers extends javax.swing.JPanel {

	private ContextNormal context;
	private HexTDTower selectedTower;

	/** Creates new form PanelEditorTowers */
	public void setContext(ContextNormal c) {
		context = c;
		initComponents();
		DefaultComboBoxModel model = new DefaultComboBoxModel(FactoryTower.type.values());
		jComboBox1.setModel(model);
		model = new DefaultComboBoxModel(FactoryTower.type.values());
		jComboBox2.setModel(model);
		refreshTowerList();
	}

	public void refreshTowerList() {
		DefaultListModel model = new DefaultListModel();
		if (context.loadedGameMap.towers == null) {
			context.loadedGameMap.towers = new ArrayList<JsonTower>();
		}
		for (JsonTower t : context.loadedGameMap.towers) {
			model.addElement(t);
		}
		jList1.setModel(model);
		model = new DefaultListModel();
		if (context.loadedGameMap.towerOptions == null) {
			context.loadedGameMap.towerOptions = new ArrayList<JsonTowerOptions>();
		}
		for (JsonTowerOptions to : context.loadedGameMap.towerOptions) {
			model.addElement(to);
		}
		jList2.setModel(model);
	}

	private void selectTower(HexTDTower t) {
		if (selectedTower != null) {
			selectedTower.setSelected(false);
			selectedTower.getHex().setHighlight(HexTile.HighlightType.none);
		}
		selectedTower = t;
		if (selectedTower != null) {
			selectedTower.setSelected(true);
			selectedTower.getHex().setHighlight(HexTile.HighlightType.selectTower);
		}
	}

	private void towerSelected(JsonTower t) {
		HexTile hex = context.getGameGrid().getHex(t.col, t.row);
		jPanelTowerSettings.removeAll();
		jPanelTowerSettings.add(t.getSettingsPanel());
		jPanelTowerSettings.repaint();
		selectTower(hex.getTower());
	}

	private void towerOptionSelected(JsonTowerOptions to) {
		jPanelTowerSettings2.removeAll();
		jPanelTowerSettings2.add(to.getSettingsPanel());
		jPanelTowerSettings2.repaint();
	}

	private void deleteTower(JsonTower t) {
		HexTile hex = context.getGameGrid().getHex(t.col, t.row);
		HexTDTower tower = hex.getTower();
		if (tower != null) {
			context.removeTower(tower);
		}
		context.loadedGameMap.towers.remove(t);
		refreshTowerList();
	}

	public void hexClicked(HexTile hex) {
		if (jToggleButtonAdd.isSelected()) {
			FactoryTower.type selectedType = (FactoryTower.type) jComboBox1.getSelectedItem();
			addTower(selectedType, hex);
			jToggleButtonAdd.setSelected(false);
		}
	}

	private void addTower(FactoryTower.type type, HexTile hex) {
		if (!hex.hasTower()) {
			JsonTower t = new JsonTower();
			t.type = type;
			t.col = hex.getCol();
			t.row = hex.getRow();
			context.loadedGameMap.towers.add(t);
			HexTDTower newTower = FactoryTower.createTower(
					t.type, context, hex, t.owner, Context.actionDelay);
			context.addTower(newTower);
			refreshTowerList();
			towerSelected(t);
		}
	}

	@Override
	public void setVisible(boolean aFlag) {
		super.setVisible(aFlag);
		if (selectedTower != null) {
			selectedTower.setSelected(aFlag);
		}
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jComboBox1 = new javax.swing.JComboBox();
        jButtonDel = new javax.swing.JButton();
        jToggleButtonAdd = new javax.swing.JToggleButton();
        jPanelTowerSettings = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        jComboBox2 = new javax.swing.JComboBox();
        jButtonDel2 = new javax.swing.JButton();
        jButtonAdd2 = new javax.swing.JButton();
        jPanelTowerSettings2 = new javax.swing.JPanel();

        setLayout(new java.awt.BorderLayout());

        jPanel1.setLayout(new java.awt.GridBagLayout());

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList1ValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jList1);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0E-5;
        gridBagConstraints.weighty = 0.04;
        jPanel1.add(jScrollPane1, gridBagConstraints);

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 0.0010;
        jPanel1.add(jComboBox1, gridBagConstraints);

        jButtonDel.setText("-");
        jButtonDel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDelActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        jPanel1.add(jButtonDel, gridBagConstraints);

        jToggleButtonAdd.setText("+");
        jToggleButtonAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButtonAddActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        jPanel1.add(jToggleButtonAdd, gridBagConstraints);

        jPanelTowerSettings.setLayout(new java.awt.BorderLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0E-4;
        gridBagConstraints.weighty = 0.04;
        jPanel1.add(jPanelTowerSettings, gridBagConstraints);

        jTabbedPane1.addTab("Prebuilt", jPanel1);

        jPanel2.setLayout(new java.awt.GridBagLayout());

        jList2.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jList2.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList2ValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(jList2);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0E-5;
        gridBagConstraints.weighty = 0.04;
        jPanel2.add(jScrollPane2, gridBagConstraints);

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 0.0010;
        jPanel2.add(jComboBox2, gridBagConstraints);

        jButtonDel2.setText("-");
        jButtonDel2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDel2ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        jPanel2.add(jButtonDel2, gridBagConstraints);

        jButtonAdd2.setText("+");
        jButtonAdd2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAdd2ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        jPanel2.add(jButtonAdd2, gridBagConstraints);

        jPanelTowerSettings2.setLayout(new java.awt.BorderLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0E-4;
        gridBagConstraints.weighty = 0.04;
        jPanel2.add(jPanelTowerSettings2, gridBagConstraints);

        jTabbedPane1.addTab("Buildable", jPanel2);

        add(jTabbedPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

	private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList1ValueChanged
		JsonTower t = (JsonTower) jList1.getSelectedValue();
		if (t != null) {
			towerSelected(t);
		}
	}//GEN-LAST:event_jList1ValueChanged

	private void jButtonDelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDelActionPerformed
		JsonTower t = (JsonTower) jList1.getSelectedValue();
		if (t != null) {
			deleteTower(t);
		}
	}//GEN-LAST:event_jButtonDelActionPerformed

	private void jToggleButtonAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButtonAddActionPerformed
	}//GEN-LAST:event_jToggleButtonAddActionPerformed

	private void jList2ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList2ValueChanged
		JsonTowerOptions to = (JsonTowerOptions) jList2.getSelectedValue();
		if (to != null) {
			towerOptionSelected(to);
		}
	}//GEN-LAST:event_jList2ValueChanged

	private void jButtonDel2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDel2ActionPerformed
		FactoryTower.type t = (FactoryTower.type) jList2.getSelectedValue();
		if (t != null) {
			context.loadedGameMap.notTowers.remove(t);
			refreshTowerList();
		}
	}//GEN-LAST:event_jButtonDel2ActionPerformed

	private void jButtonAdd2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAdd2ActionPerformed
		FactoryTower.type selectedType = (FactoryTower.type) jComboBox2.getSelectedItem();
		for (JsonTowerOptions to : context.loadedGameMap.towerOptions) {
			if (to.type == selectedType) {
				return;
			}
		}
		JsonTowerOptions to = new JsonTowerOptions();
		to.type = selectedType;
		context.loadedGameMap.towerOptions.add(to);
		refreshTowerList();
	}//GEN-LAST:event_jButtonAdd2ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAdd2;
    private javax.swing.JButton jButtonDel;
    private javax.swing.JButton jButtonDel2;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanelTowerSettings;
    private javax.swing.JPanel jPanelTowerSettings2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JToggleButton jToggleButtonAdd;
    // End of variables declaration//GEN-END:variables
}
